package com.csht.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csht.model.CshtGoods;
import com.csht.model.CshtGoodsReport;
import com.csht.model.CshtMessage;
import com.csht.model.CshtMessageReport;
import com.csht.model.CshtNotice;
import com.csht.model.CshtTrading;
import com.csht.service.CshtGoodsReportService;
import com.csht.service.CshtGoodsService;
import com.csht.service.CshtMessageReportService;
import com.csht.service.CshtMessageService;
import com.csht.service.CshtNoticeService;
import com.csht.service.CshtTradingService;
import com.csht.util.GetMessage;
import com.csht.vo.goods.GoodsDetails;

/**
 * 商品详情controller
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/goods")
public class GoodsDetalisController {
	
	@Autowired
	private CshtGoodsService cshtGoodsService;
	
	@Autowired
	private CshtTradingService cshtTradingService;
	
	@Autowired
	private CshtNoticeService cshtNoticeService;
	
	@Autowired
	private CshtGoodsReportService cshtGoodsReportService;
	
	@Autowired
	private CshtMessageReportService cshtMessageReportService;
	
	@Autowired
	private CshtMessageService cshtMessageService;

	/**
	 * 商品详细（根据商品id查找商品）
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/goodsDetails/{goodsId}")
	public String goodsDetails(ModelMap model, @PathVariable Integer goodsId) throws Exception{
		GoodsDetails goodsDetails = cshtGoodsService.getGoodsDetailData(goodsId);
		model.addAttribute("goodsDetails", goodsDetails);
		return "/goods/goodsDetails";
	}
	
	/**
	 * 商品举报
	 * @param userId
	 * @param goodsId
	 * @param reportContent
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/reportGoods")
	public Map<String, Object> reportGoods(@RequestParam(name = "userId") Integer userId, 
			@RequestParam(name = "goodsId") Integer goodsId, 
			@RequestParam(name = "value") String reportContent) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		CshtGoodsReport cshtGoodsReport = new CshtGoodsReport();
		cshtGoodsReport.setGoodsId(goodsId);
		cshtGoodsReport.setGoodsReportResults("无");
		cshtGoodsReport.setGoodsReportState("未处理");
		cshtGoodsReport.setGoodsReportTime(new java.sql.Date(System.currentTimeMillis()));
		cshtGoodsReport.setGoodsReportWhy(reportContent);
		cshtGoodsReport.setUserId(userId);
		cshtGoodsReportService.insertGoodsReport(cshtGoodsReport); //添加商品举报记录
		res.put("result", true);
		return res;
	}
	
	/**
	 * 留言举报
	 * @param messageId
	 * @param userId
	 * @param goodsId
	 * @param messageReportWhy
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/reportM")
	public Map<String, Object> reportM(@RequestParam(name = "messageId") Integer messageId, 
			@RequestParam(name = "userId") Integer userId, @RequestParam(name = "goodsId") Integer goodsId, 
			@RequestParam(name = "messageReportWhy") String messageReportWhy) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		CshtMessageReport cshtMessageReport = new CshtMessageReport();
		cshtMessageReport.setGoodsId(goodsId);
		cshtMessageReport.setMessageId(messageId);
		cshtMessageReport.setMessageReportResults("无");
		cshtMessageReport.setMessageReportState("未处理");
		cshtMessageReport.setMessageReportTime(new java.sql.Date(System.currentTimeMillis()));
		cshtMessageReport.setMessageReportWhy(messageReportWhy);
		cshtMessageReport.setUserId(userId);
		cshtMessageReportService.insertMessageReport(cshtMessageReport); //插入一条留言举报记录
		res.put("result", true);
		return res;
	}
	
	/**
	 * 发布留言
	 * @param userId
	 * @param content
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/rToMessage")
	public Map<String, Object> rToMessage(@RequestParam(name = "m_userId") Integer mUserId, 
			@RequestParam(name = "bm_userId") Integer bmUserId, 
			@RequestParam(name = "goodsId") Integer goodsId, 
			@RequestParam(name = "content") String messageContent) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		CshtMessage cshtMessage = new CshtMessage();
		cshtMessage.setBmUserId(bmUserId);
		cshtMessage.setGoodsId(goodsId);
		cshtMessage.setMessageContent(messageContent);
		cshtMessage.setMessageTime(new java.sql.Date(System.currentTimeMillis()));
		cshtMessage.setmUserId(mUserId);
		cshtMessageService.insertMessage(cshtMessage);
		res.put("result", true);
		return res;
	}
	
	/**
	 * 预订商品，并向双方发送短信通知和消息
	 * @param goodsId
	 * @param goodsUserWay
	 * @param goodsWay
	 * @param buyGoodsUserWay
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/userToBuyGoods")
	public Map<String, Object> userToBuyGoods(@RequestParam(name = "goodsId") Integer goodsId, 
			@RequestParam(name = "goodsUserWay") String goodsUserWay, 
			@RequestParam(name = "goodsWay") String goodsWay, 
			@RequestParam(name = "buyGoodsUserWay") String buyGoodsUserWay, 
			@RequestParam(name = "loginUserId") Integer loginUserId, 
			@RequestParam(name = "goodsUserId") Integer goodsUserId,
			@RequestParam(name = "loginUserTel") String loginUserTel,
			@RequestParam(name = "goodsUserTel") String goodsUserTel) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		CshtGoods cshtGoods = cshtGoodsService.selectGoodsById(goodsId); //根据id查找商品
		cshtGoodsService.updateGoodsStateTradingById(goodsId); //根据商品id更新商品状态为交易中
		CshtTrading cshtTrading = new CshtTrading(); //交易中的商品类
		//设置属性
		cshtTrading.setBrandId(cshtGoods.getBrandId());
		cshtTrading.setGoodsId(cshtGoods.getGoodsId());
		cshtTrading.setOneMenuId(cshtGoods.getOneMenuId());
		cshtTrading.setThreeMenuId(cshtGoods.getThreeMenuId());
		cshtTrading.setTradingBrowseTime(cshtGoods.getGoodsBrowseTime());
		cshtTrading.setTradingContent(cshtGoods.getGoodsContent());
		cshtTrading.setTradingImgpath(cshtGoods.getGoodsImgpath());
		cshtTrading.setTradingPayMoney(cshtGoods.getGoodsPayMoney());
		cshtTrading.setTradingPrice(cshtGoods.getGoodsPrice());
		cshtTrading.setTradingTitle(cshtGoods.getGoodsTitle());
		cshtTrading.setTradingTradeSchool(cshtGoods.getGoodsTradeSchool());
		cshtTrading.setTradingTradeWay(cshtGoods.getGoodsTradeWay());
		cshtTrading.setTwoMenuId(cshtGoods.getTwoMenuId());
		cshtTrading.setUserId(cshtGoods.getUserId());
		cshtTradingService.insertTradingGoods(cshtTrading); //向交易中的商品插入一条记录
		java.sql.Date time = new java.sql.Date(System.currentTimeMillis());
		//买家的信息内容
		String buyUserNoticeContent = "尊敬的用户，您好，您于" + time + 
				"预订了商品【" + cshtGoods.getGoodsTitle() + "】，卖家的" + goodsWay + "号为：" + goodsUserWay + 
				"，请及时联系以保证交易的正常进行。";
		//卖家的信息内容
		String goodsUserNoticeContent = "尊敬的用户，您好，您的商品【" + cshtGoods.getGoodsTitle() + "】于" + 
				time + "已被预订，买家的" + goodsWay + "号为：" + buyGoodsUserWay + 
				"，请及时联系以保证交易的正常进行。如交易不能进行，可在个人中心－交易中的商品中再次上架交易失败的商品。"
				+ "如交易成功，可在个人中心－交易中的商品中手动点击交易完成。";
		CshtNotice cshtNotice_1 = new CshtNotice(); //买家消息通知
		CshtNotice cshtNotice_2 = new CshtNotice(); //卖家消息通知
		cshtNotice_1.setNoticeContent(buyUserNoticeContent);
		cshtNotice_1.setNoticeStuta("未读");
		cshtNotice_1.setNoticeTime(time);
		cshtNotice_1.setNoticeTitle("商品预订通知");
		cshtNotice_1.setUserId(loginUserId);
		cshtNotice_2.setNoticeContent(goodsUserNoticeContent);
		cshtNotice_2.setNoticeStuta("未读");
		cshtNotice_2.setNoticeTime(time);
		cshtNotice_2.setNoticeTitle("商品预订通知");
		cshtNotice_2.setUserId(goodsUserId);
		cshtNoticeService.insertNotice(cshtNotice_1); //给买家发消息通知
		cshtNoticeService.insertNotice(cshtNotice_2); //给卖家发消息通知
		//申请通知模板没通过，暂时(注册模板)
		GetMessage.getResult(loginUserTel, "register"); //给买家发短信
		GetMessage.getResult(goodsUserTel, "register"); //给卖家发短信
		res.put("result", true);
		return res;
	}
	
}
